<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class BannerController extends AdminbaseController{
    
	protected $banner_model;
	protected $classify_model;

	public function _initialize() {
		parent::_initialize();
		$this->banner_model = D("Common/Banner");
		$this->classify_model = D("Common/Classify");
	}
	
	// 后台Banner列表
	public function index(){
       $sql = " SELECT cb.*,cf.`name` cfname FROM cmf_banner cb
                LEFT JOIN cmf_classify cf ON cf.id = cb.classify_id
                ORDER BY cb.listorder ";
		$bannerss =$this->banner_model->query($sql);
        foreach($bannerss as $vo){
            if( $vo['classify_id'] == 0  ){
                $vo['cfname'] = "首页";
            }
            $banners[] = $vo;
        }
		$this->assign("banners",$banners);
		$this->display();
	}
	
	// Banner添加
	public function add(){
        // 查询所有一级分类
        $classifies=$this->classify_model->where('pid = 0')->select();
        $this->assign('classifies',$classifies);
		$this->display();
	}
	
	// Banner添加提交
	public function add_post(){
        $_POST['create_time'] = date("Y-m-d H:i:s", time());
        $_POST['creator_id'] = get_current_admin_id();
		if(IS_POST){
			if ($this->banner_model->create()!==false){
                $id = $this->banner_model->add();
				if ($id!==false) {
                    $this->banner_model->where(array('id'=>$id))->save(array('update_time' => date("Y-m-d H:i:s", time())));
					$this->success(L('ADD_SUCCESS'), U("Banner/index"));
				} else {
					$this->error(L('ADD_FAILED'));
				}
			} else {
				$this->error($this->banner_model->getError());
			}
		
		}
	}
	
	// Banner编辑
	public function edit(){
		$id=I("get.id",0,'intval');
		$banner = $this->banner_model->where(array('id'=>$id))->find();
        //        查询所有一级分类
        $classifiess = $this->classify_model->where('pid = 0')->select();
        foreach ($classifiess as $vo){
            if($vo['id'] == $banner['classify_id']){
                $vo['selected'] = 'selected';
            }else{
                $vo['selected'] = '';
            }
                $classifies[] = $vo;
        }
        $banner['classifies'] = $classifies;
		$this->assign($banner);

		$this->display();
	}
	
	// Banner编辑提交
	public function edit_post(){
        $_POST['update_time'] = date("Y-m-d H:i:s", time());
        $_POST['updator_id'] = get_current_admin_id();
		if (IS_POST) {
			if ($this->banner_model->create()!==false) {
				if ($this->banner_model->save()!==false) {
					$this->success("保存成功！", U("Banner/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->banner_model->getError());
			}
		}
	}
	
	// Banner删除
	public function delete(){
		$id = I("get.id",0,"intval");
		if ($this->banner_model->delete($id)!==false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}

    //Banner排序
    public function listorders() {
        $status = parent::_listorders($this->banner_model);
        if ($status) {
            $this->success("排序更新成功！");
        } else {
            $this->error("排序更新失败！");
        }
    }
	
}